package com.czl.module_storehouse.activity.inventory.home;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.czl.base.binding.viewadapter.flycoTabLayout.TabEntity;
import com.czl.base.config.AppConstants;
import com.czl.module_base.activity.BaseTabActivity;
import com.czl.module_service.event.BorrowHomeEvent;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.event.DetailsPostSuccessEvent;
import com.czl.module_storehouse.event.InventorySuccessEvent;
import com.czl.module_storehouse.event.InventoryUpdateEvent;
import com.czl.module_storehouse.fragment.InventoryFragment;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.jeremyliao.liveeventbus.LiveEventBus;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import me.yokeyword.fragmentation.SupportFragment;

/**
 * @author xubin
 */
@Route(path = AppConstants.Router.Service.F_SERVICE_INVENTORY_HOME)
public class InventoryHomeActivity extends BaseTabActivity {


    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mToolBinding.toolbarContentTitle.setText("仓库盘点");
        EventBus.getDefault().register(this);
        LiveEventBus.get(DetailsPostSuccessEvent.class).observe(this, event -> new Handler(Looper.myLooper()).postDelayed(() -> {
            binding.viewPager2.setCurrentItem(1);
            LiveEventBus.get(InventorySuccessEvent.class).post(new InventorySuccessEvent());
        }, 200));
    }

    @Override
    protected List<SupportFragment> fragments() {
        List<SupportFragment> fragmentList = new ArrayList<>();
        fragmentList.add(InventoryFragment.newInstance(0));
        fragmentList.add(InventoryFragment.newInstance(1));
        return fragmentList;
    }

    @Override
    protected ArrayList<CustomTabEntity> mTabEntities() {
        ArrayList<CustomTabEntity> entities = new ArrayList<>();
        entities.add(new TabEntity("待盘", R.mipmap.ic_add, R.mipmap.ic_add));
        entities.add(new TabEntity("已盘", R.mipmap.ic_add, R.mipmap.ic_add));
        return entities;
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(InventoryUpdateEvent event) {
        //已完成盘点任务
        if (event != null && event.isCompleted()) {
            binding.viewPager2.setCurrentItem(1);
        }
    }

}
